package com.wondersgroup.insurance.dnuilearn.views.shader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

/**
 * <b>Project:</b> dnuilearn<br>
 * <b>Create Date:</b> 2017/5/11 0011<br>
 * <b>Author:</b> huang saisai <br>
 * <b>Description:</b> <br>
 */
public class ZoomImage extends AppCompatImageView {


    final int SCALE_RADIUS = 60;

    Bitmap bmp;
    BitmapShader  bmpShader;

    Rect scaleReact = null;


    Paint mPaint;

    int dx , dy ;


    ShapeDrawable shapeDrawable = null;

    int factor = 2;

    public ZoomImage(Context context) {
        this(context, null);
    }

    public ZoomImage(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZoomImage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        if (scaleReact != null){
//            canvas.save();
//            canvas.translate(dx-SCALE_RADIUS,dy-SCALE_RADIUS);
//            canvas.drawOval(new RectF(scaleReact), mPaint);
//            canvas.restore();
//        }

        if (shapeDrawable != null){
            shapeDrawable.draw(canvas);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        return myGesture.onTouchEvent(event);

        move(event);

        return  true;
    }


    void move(MotionEvent event){
        Drawable drawable = getDrawable();
        if (drawable == null){
            return;
        }


        if (shapeDrawable == null){

            if (bmp == null){
//            bmp = BitmapFactory.decodeResource(getResources(), R.drawable.xyjy3);
                bmp = drawableToBitmap(drawable);
            }

            Matrix matrix = new Matrix();
            matrix.setScale(factor,factor);
            bmp =  Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true );
            bmpShader = new BitmapShader(bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            shapeDrawable = new ShapeDrawable(new OvalShape());
            shapeDrawable.getPaint().setShader(bmpShader);


        }
        int x = (int) event.getX();
        int y = (int) event.getY();
        Matrix matrix = new Matrix();

        matrix.setTranslate(SCALE_RADIUS - x*factor, SCALE_RADIUS-y*factor);
        shapeDrawable.getPaint().getShader().setLocalMatrix(matrix);

        shapeDrawable.setBounds(new Rect(x-SCALE_RADIUS,y-SCALE_RADIUS,x+SCALE_RADIUS, y+SCALE_RADIUS));
        invalidate();
    }

//    GestureDetector myGesture = new GestureDetector(getContext(), new GestureDetector.OnGestureListener() {
//        @Override
//        public boolean onDown(MotionEvent motionEvent) {
//            Log.e("","onDown");
//            process(motionEvent);
//            return true;
//        }
//// 用户轻触触摸屏，尚未松开或拖动，由一个1个MotionEvent ACTION_DOWN触发
//        // 注意和onDown()的区别，强调的是没有松开或者拖动的状态
//        @Override
//        public void onShowPress(MotionEvent motionEvent) {
//            Log.e("","onShowPress");
//        }
//        // 用户（轻触触摸屏后）松开，由一个1个MotionEvent ACTION_UP触发
//        @Override
//        public boolean onSingleTapUp(MotionEvent motionEvent) {
//            Log.e("","onSingleTapUp");
//            scaleReact = null;
//            invalidate();
//            return true;
//        }
//        //按住然后滑动时调用
//        @Override
//        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
//            Log.e("","onScroll");
//
////            process(motionEvent);
//            return true;
//        }
//        //长按的时候调用
//        @Override
//        public void onLongPress(MotionEvent motionEvent) {
//
//        }
//        //手指在触摸屏上迅速移动，并松开的动作。
//        @Override
//        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
//
//            scaleReact = null;
//            invalidate();
//            return true;
//        }
//
//
//    });


//    void process(MotionEvent event){
//
//        Drawable drawable = getDrawable();
//        if (drawable == null){
//            return;
//        }
//        int x = (int) event.getX();
//        int y = (int) event.getY();
//
//        if (x < SCALE_RADIUS || y < SCALE_RADIUS){
//            return;
//        }
//        int width = drawable.getBounds().width();
//        int height = drawable.getBounds().height();
//        if (x >width || y > height){
//            return;
//        }
//
//        if (bmp == null){
////            bmp = BitmapFactory.decodeResource(getResources(), R.drawable.xyjy3);
//            bmp = drawableToBitmap(drawable);
//        }
//        bmpShader = new BitmapShader(Bitmap.createBitmap(bmp,x-20,y-20,40,40 ), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
//        scaleReact = new Rect(x-SCALE_RADIUS, y-SCALE_RADIUS,x+SCALE_RADIUS,y+SCALE_RADIUS);
//
//        Log.e("", "x = "+x+ "   y  = " + y);
//        Log.e("","scaleReact.left = "+scaleReact.left + "scaleReact.top = "+ scaleReact.top +  "scaleReact.width() = "+scaleReact.width());
//
//        scaleReact = new Rect(0,0,SCALE_RADIUS*2,SCALE_RADIUS*2);
//
//        dx = x;
//                dy = y;
//
//        Matrix matrix = new Matrix();
//        matrix.setScale(3,3);
//
//
//        bmpShader.setLocalMatrix(matrix);
//        mPaint.setShader(bmpShader);
//        invalidate();
//
//
////        Log.e("", "width = "+drawable.getBounds().width() + "height = "+drawable.getBounds().height());
//
//    }

    public static Bitmap drawableToBitmap(Drawable drawable) {



        Bitmap bitmap = Bitmap.createBitmap(

                drawable.getIntrinsicWidth(),

                drawable.getIntrinsicHeight(),

                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

                                                            : Bitmap.Config.RGB_565);

        Canvas canvas = new Canvas(bitmap);

        //canvas.setBitmap(bitmap);

        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

        drawable.draw(canvas);

        return bitmap;

    }
}
